package com.ruoyi.web.controller.base;

import cn.hutool.core.lang.Snowflake;
import com.ruoyi.base.domain.BasePro;
import com.ruoyi.base.domain.vo.ProVO;
import com.ruoyi.base.service.BaseProService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.DateUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
public class BaseProController extends BaseController {

    @Autowired
    private BaseProService baseProService;

    @Autowired
    private Snowflake snowflake;

    @PostMapping("/base/pro/add")
    public AjaxResult add(@RequestBody ProVO vo) {
        BasePro basePro = new BasePro();
        // 将 VO 的数据 拷贝到 basePro
        BeanUtils.copyProperties(vo, basePro);
        // 分布式唯一 id : 雪花算法
        basePro.setProductCode(snowflake.nextIdStr());
        basePro.setCreateTime(DateUtils.getNowDate());
        baseProService.saveBasePro(basePro);
        return success();
    }

    //  "/base/pro/list"
    @PostMapping("/base/pro/list")
    public TableDataInfo list(@RequestBody(required = false) ProVO vo) {
        startPage(); // 设置分页信息
        List<BasePro> list = baseProService.list(vo);

        return getDataTable(list);
    }

}
